#!/usr/bin/python
#encoding=utf-8

'''
Created on May 16, 2014

@author: root
'''

import re
import sys
import urllib
import uuid
from wm.etc.config import DIRS

try:
    from bs4 import BeautifulSoup
    from bs4.element import Tag
except ImportError:
    # BeautifulSoup 4.x not installed trying BeautifulSoup 3.x 
    try:
        from BeautifulSoup import BeautifulSoup
    except ImportError:
        print ('BeautifulSoup not installed')
        exit(-1)




    
class Parser(object):
    '''
    A simple HTML language parser.
    '''
    debug = True
    
    def __init__(self, file_path,eurl):
        self.file_path = file_path
        self._eurl = eurl
        
    def download_file_content(self):
        try:
            suffix = ''
            index = self._eurl.rfind(".")
            if index != -1:
                suffix = self._eurl[index+1:len(self._eurl)]
            fname = self.file_path + uuid.uuid1().__str__().replace("-","") + "." + suffix
            webpage = urllib.urlopen(self._eurl)
            f = open(fname,'wb')
            f.write(webpage.read())
            f.close()
            return fname
        except:
            print 'get url content error'
            raise
        
        return ''
        
    
    
if __name__ == '__main__':
    pass